package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PageInfo;
import com.genesis.mall.entity.SmsFlashPromotion;
import com.genesis.mall.mapper.SmsFlashPromotionMapper;
import com.genesis.mall.service.ISmsFlashPromotionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 限时购表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class SmsFlashPromotionServiceImpl extends ServiceImpl<SmsFlashPromotionMapper, SmsFlashPromotion> implements ISmsFlashPromotionService {

    /**
     * 根据活动名称分页
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo pageFlash(String keyword, Integer pageNum, Integer pageSize) {
        Page<SmsFlashPromotion> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(keyword),SmsFlashPromotion::getTitle,keyword);
        Page<SmsFlashPromotion> flashPage = this.page(page, lqw);
        int total = (int) flashPage.getTotal();
        List<SmsFlashPromotion> flashPromotionList = flashPage.getRecords();
        Integer totalPage = PageInfo.totalPage(pageSize, total);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(flashPromotionList);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(totalPage);

        return pageInfo;
    }

    /**
     * 修改上下线的状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        SmsFlashPromotion flashPromotion = this.getById(id);
        flashPromotion.setStatus(status);
        this.updateById(flashPromotion);
    }
}
